<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.utils.functional &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-functional">
            
  <h1>Source code for django.utils.functional</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">operator</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">wraps</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="kn">import</span> <span class="n">RemovedInDjango19Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves</span> <span class="kn">import</span> <span class="n">copyreg</span>


<span class="c"># You can&#39;t trivially replace this with `functools.partial` because this binds</span>
<span class="c"># to classes and returns bound instances, whereas functools.partial (on</span>
<span class="c"># CPython) is a type and its instances don&#39;t bind.</span>
<span class="k">def</span> <span class="nf">curry</span><span class="p">(</span><span class="n">_curried_func</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">_curried</span><span class="p">(</span><span class="o">*</span><span class="n">moreargs</span><span class="p">,</span> <span class="o">**</span><span class="n">morekwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">_curried_func</span><span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">args</span> <span class="o">+</span> <span class="n">moreargs</span><span class="p">),</span> <span class="o">**</span><span class="nb">dict</span><span class="p">(</span><span class="n">kwargs</span><span class="p">,</span> <span class="o">**</span><span class="n">morekwargs</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">_curried</span>


<span class="k">def</span> <span class="nf">memoize</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">cache</span><span class="p">,</span> <span class="n">num_args</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Wrap a function so that results for any argument tuple are stored in</span>
<span class="sd">    &#39;cache&#39;. Note that the args to the function must be usable as dictionary</span>
<span class="sd">    keys.</span>

<span class="sd">    Only the first num_args are considered when creating the key.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;memoize wrapper is deprecated and will be removed in &quot;</span>
                  <span class="s">&quot;Django 1.9. Use django.utils.lru_cache instead.&quot;</span><span class="p">,</span>
                  <span class="n">RemovedInDjango19Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>

    <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="n">mem_args</span> <span class="o">=</span> <span class="n">args</span><span class="p">[:</span><span class="n">num_args</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">mem_args</span> <span class="ow">in</span> <span class="n">cache</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">cache</span><span class="p">[</span><span class="n">mem_args</span><span class="p">]</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
        <span class="n">cache</span><span class="p">[</span><span class="n">mem_args</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span>
        <span class="k">return</span> <span class="n">result</span>
    <span class="k">return</span> <span class="n">wrapper</span>


<div class="viewcode-block" id="cached_property"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.functional.cached_property">[docs]</a><span class="k">class</span> <span class="nc">cached_property</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Decorator that converts a method with a single self argument into a</span>
<span class="sd">    property cached on the instance.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="n">func</span>

    <span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="n">res</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">__dict__</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="o">.</span><span class="n">__name__</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">res</span>

</div>
<span class="k">class</span> <span class="nc">Promise</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This is just a base class for the proxy class created in</span>
<span class="sd">    the closure of the lazy function. It can be used to recognize</span>
<span class="sd">    promises in code.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span>


<span class="k">def</span> <span class="nf">lazy</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">resultclasses</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Turns any callable into a lazy evaluated callable. You need to give result</span>
<span class="sd">    classes or types -- at least one is needed so that the automatic forcing of</span>
<span class="sd">    the lazy evaluation code is triggered. Results are not memoized; the</span>
<span class="sd">    function is evaluated on every access.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@total_ordering</span>
    <span class="k">class</span> <span class="nc">__proxy__</span><span class="p">(</span><span class="n">Promise</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Encapsulate a function call and act as a proxy for methods that are</span>
<span class="sd">        called on the result of that function. The function is not evaluated</span>
<span class="sd">        until one of the methods on the result is called.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">__dispatch</span> <span class="o">=</span> <span class="bp">None</span>

        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kw</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__args</span> <span class="o">=</span> <span class="n">args</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__kw</span> <span class="o">=</span> <span class="n">kw</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dispatch</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">__prepare_class__</span><span class="p">()</span>

        <span class="k">def</span> <span class="nf">__reduce__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">(</span>
                <span class="n">_lazy_proxy_unpickle</span><span class="p">,</span>
                <span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__args</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__kw</span><span class="p">)</span> <span class="o">+</span> <span class="n">resultclasses</span>
            <span class="p">)</span>

        <span class="nd">@classmethod</span>
        <span class="k">def</span> <span class="nf">__prepare_class__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
            <span class="n">cls</span><span class="o">.</span><span class="n">__dispatch</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">resultclass</span> <span class="ow">in</span> <span class="n">resultclasses</span><span class="p">:</span>
                <span class="n">cls</span><span class="o">.</span><span class="n">__dispatch</span><span class="p">[</span><span class="n">resultclass</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
                <span class="k">for</span> <span class="n">type_</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">resultclass</span><span class="o">.</span><span class="n">mro</span><span class="p">()):</span>
                    <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">type_</span><span class="o">.</span><span class="n">__dict__</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                        <span class="c"># All __promise__ return the same wrapper method, but</span>
                        <span class="c"># they also do setup, inserting the method into the</span>
                        <span class="c"># dispatch dict.</span>
                        <span class="n">meth</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">__promise__</span><span class="p">(</span><span class="n">resultclass</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
                        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
                            <span class="k">continue</span>
                        <span class="nb">setattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">meth</span><span class="p">)</span>
            <span class="n">cls</span><span class="o">.</span><span class="n">_delegate_bytes</span> <span class="o">=</span> <span class="nb">bytes</span> <span class="ow">in</span> <span class="n">resultclasses</span>
            <span class="n">cls</span><span class="o">.</span><span class="n">_delegate_text</span> <span class="o">=</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span> <span class="ow">in</span> <span class="n">resultclasses</span>
            <span class="k">assert</span> <span class="ow">not</span> <span class="p">(</span><span class="n">cls</span><span class="o">.</span><span class="n">_delegate_bytes</span> <span class="ow">and</span> <span class="n">cls</span><span class="o">.</span><span class="n">_delegate_text</span><span class="p">),</span> <span class="s">&quot;Cannot call lazy() with both bytes and text return types.&quot;</span>
            <span class="k">if</span> <span class="n">cls</span><span class="o">.</span><span class="n">_delegate_text</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY3</span><span class="p">:</span>
                    <span class="n">cls</span><span class="o">.</span><span class="n">__str__</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">__text_cast</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">cls</span><span class="o">.</span><span class="n">__unicode__</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">__text_cast</span>
            <span class="k">elif</span> <span class="n">cls</span><span class="o">.</span><span class="n">_delegate_bytes</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY3</span><span class="p">:</span>
                    <span class="n">cls</span><span class="o">.</span><span class="n">__bytes__</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">__bytes_cast</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">cls</span><span class="o">.</span><span class="n">__str__</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">__bytes_cast</span>

        <span class="nd">@classmethod</span>
        <span class="k">def</span> <span class="nf">__promise__</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">klass</span><span class="p">,</span> <span class="n">funcname</span><span class="p">,</span> <span class="n">method</span><span class="p">):</span>
            <span class="c"># Builds a wrapper around some magic method and registers that</span>
            <span class="c"># magic method for the given type and method name.</span>
            <span class="k">def</span> <span class="nf">__wrapper__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
                <span class="c"># Automatically triggers the evaluation of a lazy value and</span>
                <span class="c"># applies the given magic method of the result type.</span>
                <span class="n">res</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">__args</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">__kw</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">type</span><span class="p">(</span><span class="n">res</span><span class="p">)</span><span class="o">.</span><span class="n">mro</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dispatch</span><span class="p">:</span>
                        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dispatch</span><span class="p">[</span><span class="n">t</span><span class="p">][</span><span class="n">funcname</span><span class="p">](</span><span class="n">res</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;Lazy object returned unexpected type.&quot;</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">klass</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">cls</span><span class="o">.</span><span class="n">__dispatch</span><span class="p">:</span>
                <span class="n">cls</span><span class="o">.</span><span class="n">__dispatch</span><span class="p">[</span><span class="n">klass</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">cls</span><span class="o">.</span><span class="n">__dispatch</span><span class="p">[</span><span class="n">klass</span><span class="p">][</span><span class="n">funcname</span><span class="p">]</span> <span class="o">=</span> <span class="n">method</span>
            <span class="k">return</span> <span class="n">__wrapper__</span>

        <span class="k">def</span> <span class="nf">__text_cast</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">__args</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">__kw</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">__bytes_cast</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">__args</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">__kw</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">__cast</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delegate_bytes</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__bytes_cast</span><span class="p">()</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delegate_text</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__text_cast</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">__args</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">__kw</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">__ne__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Promise</span><span class="p">):</span>
                <span class="n">other</span> <span class="o">=</span> <span class="n">other</span><span class="o">.</span><span class="n">__cast</span><span class="p">()</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__cast</span><span class="p">()</span> <span class="o">!=</span> <span class="n">other</span>

        <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Promise</span><span class="p">):</span>
                <span class="n">other</span> <span class="o">=</span> <span class="n">other</span><span class="o">.</span><span class="n">__cast</span><span class="p">()</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__cast</span><span class="p">()</span> <span class="o">==</span> <span class="n">other</span>

        <span class="k">def</span> <span class="nf">__lt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Promise</span><span class="p">):</span>
                <span class="n">other</span> <span class="o">=</span> <span class="n">other</span><span class="o">.</span><span class="n">__cast</span><span class="p">()</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__cast</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">other</span>

        <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">hash</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__cast</span><span class="p">())</span>

        <span class="k">def</span> <span class="nf">__mod__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">rhs</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delegate_bytes</span> <span class="ow">and</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
                <span class="k">return</span> <span class="nb">bytes</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">%</span> <span class="n">rhs</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delegate_text</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">%</span> <span class="n">rhs</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__cast</span><span class="p">()</span> <span class="o">%</span> <span class="n">rhs</span>

        <span class="k">def</span> <span class="nf">__deepcopy__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">memo</span><span class="p">):</span>
            <span class="c"># Instances of this class are effectively immutable. It&#39;s just a</span>
            <span class="c"># collection of functions. So we don&#39;t need to do anything</span>
            <span class="c"># complicated for copying.</span>
            <span class="n">memo</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)]</span> <span class="o">=</span> <span class="bp">self</span>
            <span class="k">return</span> <span class="bp">self</span>

    <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">__wrapper__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
        <span class="c"># Creates the proxy object, instead of the actual value.</span>
        <span class="k">return</span> <span class="n">__proxy__</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kw</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">__wrapper__</span>


<span class="k">def</span> <span class="nf">_lazy_proxy_unpickle</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="o">*</span><span class="n">resultclasses</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">lazy</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">resultclasses</span><span class="p">)(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>


<div class="viewcode-block" id="allow_lazy"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.functional.allow_lazy">[docs]</a><span class="k">def</span> <span class="nf">allow_lazy</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">resultclasses</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A decorator that allows a function to be called with one or more lazy</span>
<span class="sd">    arguments. If none of the args are lazy, the function is evaluated</span>
<span class="sd">    immediately, otherwise a __proxy__ is returned that will evaluate the</span>
<span class="sd">    function when needed.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">itervalues</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)):</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">Promise</span><span class="p">):</span>
                <span class="k">break</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">lazy</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">resultclasses</span><span class="p">)(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">wrapper</span>
</div>
<span class="n">empty</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">new_method_proxy</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="ow">is</span> <span class="n">empty</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_setup</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">inner</span>


<span class="k">class</span> <span class="nc">LazyObject</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A wrapper for another class that can be used to delay instantiation of the</span>
<span class="sd">    wrapped class.</span>

<span class="sd">    By subclassing, you have the opportunity to intercept and alter the</span>
<span class="sd">    instantiation. If you don&#39;t need to do that, use SimpleLazyObject.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c"># Avoid infinite recursion when tracing __init__ (#19456).</span>
    <span class="n">_wrapped</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="o">=</span> <span class="n">empty</span>

    <span class="n">__getattr__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">getattr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__setattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s">&quot;_wrapped&quot;</span><span class="p">:</span>
            <span class="c"># Assign to __dict__ to avoid infinite __setattr__ loops.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="p">[</span><span class="s">&quot;_wrapped&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="ow">is</span> <span class="n">empty</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_setup</span><span class="p">()</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__delattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s">&quot;_wrapped&quot;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;can&#39;t delete _wrapped.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="ow">is</span> <span class="n">empty</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_setup</span><span class="p">()</span>
        <span class="nb">delattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Must be implemented by subclasses to initialize the wrapped object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s">&#39;subclasses of LazyObject must provide a _setup() method&#39;</span><span class="p">)</span>

    <span class="c"># Because we have messed with __class__ below, we confuse pickle as to what</span>
    <span class="c"># class we are pickling. It also appears to stop __reduce__ from being</span>
    <span class="c"># called. So, we define __getstate__ in a way that cooperates with the way</span>
    <span class="c"># that pickle interprets this class.  This fails when the wrapped class is</span>
    <span class="c"># a builtin, but it is better than nothing.</span>
    <span class="k">def</span> <span class="nf">__getstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="ow">is</span> <span class="n">empty</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_setup</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="o">.</span><span class="n">__dict__</span>

    <span class="c"># Python 3.3 will call __reduce__ when pickling; this method is needed</span>
    <span class="c"># to serialize and deserialize correctly.</span>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__newobj__</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__reduce_ex__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">proto</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">proto</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">:</span>
            <span class="c"># On Py3, since the default protocol is 3, pickle uses the</span>
            <span class="c"># ``__newobj__`` method (&amp; more efficient opcodes) for writing.</span>
            <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__newobj__</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="p">,),</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getstate__</span><span class="p">())</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># On Py2, the default protocol is 0 (for back-compat) &amp; the above</span>
            <span class="c"># code fails miserably (see regression test). Instead, we return</span>
            <span class="c"># exactly what&#39;s returned if there&#39;s no ``__reduce__`` method at</span>
            <span class="c"># all.</span>
            <span class="k">return</span> <span class="p">(</span><span class="n">copyreg</span><span class="o">.</span><span class="n">_reconstructor</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="nb">object</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getstate__</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">__deepcopy__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">memo</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="ow">is</span> <span class="n">empty</span><span class="p">:</span>
            <span class="c"># We have to use type(self), not self.__class__, because the</span>
            <span class="c"># latter is proxied.</span>
            <span class="n">result</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)()</span>
            <span class="n">memo</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)]</span> <span class="o">=</span> <span class="n">result</span>
            <span class="k">return</span> <span class="n">result</span>
        <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="p">,</span> <span class="n">memo</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY3</span><span class="p">:</span>
        <span class="n">__bytes__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">bytes</span><span class="p">)</span>
        <span class="n">__str__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
        <span class="n">__bool__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">bool</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">__str__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
        <span class="n">__unicode__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">unicode</span><span class="p">)</span>
        <span class="n">__nonzero__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">bool</span><span class="p">)</span>

    <span class="c"># Introspection support</span>
    <span class="n">__dir__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">dir</span><span class="p">)</span>

    <span class="c"># Need to pretend to be the wrapped class, for the sake of objects that</span>
    <span class="c"># care about this (especially in equality tests)</span>
    <span class="n">__class__</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">new_method_proxy</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">attrgetter</span><span class="p">(</span><span class="s">&quot;__class__&quot;</span><span class="p">)))</span>
    <span class="n">__eq__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">eq</span><span class="p">)</span>
    <span class="n">__ne__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">ne</span><span class="p">)</span>
    <span class="n">__hash__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">hash</span><span class="p">)</span>

    <span class="c"># Dictionary methods support</span>
    <span class="n">__getitem__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">getitem</span><span class="p">)</span>
    <span class="n">__setitem__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">setitem</span><span class="p">)</span>
    <span class="n">__delitem__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">delitem</span><span class="p">)</span>

    <span class="n">__len__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="nb">len</span><span class="p">)</span>
    <span class="n">__contains__</span> <span class="o">=</span> <span class="n">new_method_proxy</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">contains</span><span class="p">)</span>


<span class="c"># Workaround for http://bugs.python.org/issue12370</span>
<span class="n">_super</span> <span class="o">=</span> <span class="nb">super</span>


<span class="k">class</span> <span class="nc">SimpleLazyObject</span><span class="p">(</span><span class="n">LazyObject</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A lazy object initialized from any function.</span>

<span class="sd">    Designed for compound objects of unknown type. For builtins or objects of</span>
<span class="sd">    known type, use django.utils.functional.lazy.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Pass in a callable that returns the object to be wrapped.</span>

<span class="sd">        If copies are made of the resulting SimpleLazyObject, which can happen</span>
<span class="sd">        in various circumstances within Django, then you must ensure that the</span>
<span class="sd">        callable can be safely run more than once and will return the same</span>
<span class="sd">        value.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="p">[</span><span class="s">&#39;_setupfunc&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">func</span>
        <span class="n">_super</span><span class="p">(</span><span class="n">SimpleLazyObject</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_setupfunc</span><span class="p">()</span>

    <span class="c"># Return a meaningful representation of the lazy object for debugging</span>
    <span class="c"># without evaluating the wrapped object.</span>
    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="ow">is</span> <span class="n">empty</span><span class="p">:</span>
            <span class="n">repr_attr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_setupfunc</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">repr_attr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span>
        <span class="k">return</span> <span class="s">&#39;&lt;</span><span class="si">%s</span><span class="s">: </span><span class="si">%r</span><span class="s">&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">repr_attr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__deepcopy__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">memo</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span> <span class="ow">is</span> <span class="n">empty</span><span class="p">:</span>
            <span class="c"># We have to use SimpleLazyObject, not self.__class__, because the</span>
            <span class="c"># latter is proxied.</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">SimpleLazyObject</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_setupfunc</span><span class="p">)</span>
            <span class="n">memo</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)]</span> <span class="o">=</span> <span class="n">result</span>
            <span class="k">return</span> <span class="n">result</span>
        <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="p">,</span> <span class="n">memo</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">lazy_property</span><span class="p">(</span><span class="nb">property</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A property that works with subclasses by wrapping the decorated</span>
<span class="sd">    functions of the base class.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">fget</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">fset</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">fdel</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">fget</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="nd">@wraps</span><span class="p">(</span><span class="n">fget</span><span class="p">)</span>
            <span class="k">def</span> <span class="nf">fget</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">instance_type</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">fget</span><span class="o">.</span><span class="n">__name__</span><span class="p">):</span>
                <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">name</span><span class="p">)()</span>
        <span class="k">if</span> <span class="n">fset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="nd">@wraps</span><span class="p">(</span><span class="n">fset</span><span class="p">)</span>
            <span class="k">def</span> <span class="nf">fset</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">fset</span><span class="o">.</span><span class="n">__name__</span><span class="p">):</span>
                <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">name</span><span class="p">)(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">fdel</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="nd">@wraps</span><span class="p">(</span><span class="n">fdel</span><span class="p">)</span>
            <span class="k">def</span> <span class="nf">fdel</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">fdel</span><span class="o">.</span><span class="n">__name__</span><span class="p">):</span>
                <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">name</span><span class="p">)()</span>
        <span class="k">return</span> <span class="nb">property</span><span class="p">(</span><span class="n">fget</span><span class="p">,</span> <span class="n">fset</span><span class="p">,</span> <span class="n">fdel</span><span class="p">,</span> <span class="n">doc</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">partition</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Splits the values into two sets, based on the return value of the function</span>
<span class="sd">    (True/False). e.g.:</span>

<span class="sd">        &gt;&gt;&gt; partition(lambda x: x &gt; 3, range(5))</span>
<span class="sd">        [0, 1, 2, 3], [4]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">results</span> <span class="o">=</span> <span class="p">([],</span> <span class="p">[])</span>
    <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">values</span><span class="p">:</span>
        <span class="n">results</span><span class="p">[</span><span class="n">predicate</span><span class="p">(</span><span class="n">item</span><span class="p">)]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">results</span>

<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
    <span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">total_ordering</span>
<span class="k">else</span><span class="p">:</span>
    <span class="c"># For Python &lt; 2.7.2. total_ordering in versions prior to 2.7.2 is buggy.</span>
    <span class="c"># See http://bugs.python.org/issue10042 for details. For these versions use</span>
    <span class="c"># code borrowed from Python 2.7.3.</span>
    <span class="k">def</span> <span class="nf">total_ordering</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Class decorator that fills in missing ordering methods&quot;&quot;&quot;</span>
        <span class="n">convert</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;__lt__&#39;</span><span class="p">:</span> <span class="p">[(</span><span class="s">&#39;__gt__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span> <span class="o">&lt;</span> <span class="n">other</span> <span class="ow">or</span> <span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">)),</span>
                       <span class="p">(</span><span class="s">&#39;__le__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="bp">self</span> <span class="o">&lt;</span> <span class="n">other</span> <span class="ow">or</span> <span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">),</span>
                       <span class="p">(</span><span class="s">&#39;__ge__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="ow">not</span> <span class="bp">self</span> <span class="o">&lt;</span> <span class="n">other</span><span class="p">)],</span>
            <span class="s">&#39;__le__&#39;</span><span class="p">:</span> <span class="p">[(</span><span class="s">&#39;__ge__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="ow">not</span> <span class="bp">self</span> <span class="o">&lt;=</span> <span class="n">other</span> <span class="ow">or</span> <span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">),</span>
                       <span class="p">(</span><span class="s">&#39;__lt__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="bp">self</span> <span class="o">&lt;=</span> <span class="n">other</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">),</span>
                       <span class="p">(</span><span class="s">&#39;__gt__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="ow">not</span> <span class="bp">self</span> <span class="o">&lt;=</span> <span class="n">other</span><span class="p">)],</span>
            <span class="s">&#39;__gt__&#39;</span><span class="p">:</span> <span class="p">[(</span><span class="s">&#39;__lt__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span> <span class="o">&gt;</span> <span class="n">other</span> <span class="ow">or</span> <span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">)),</span>
                       <span class="p">(</span><span class="s">&#39;__ge__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="bp">self</span> <span class="o">&gt;</span> <span class="n">other</span> <span class="ow">or</span> <span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">),</span>
                       <span class="p">(</span><span class="s">&#39;__le__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="ow">not</span> <span class="bp">self</span> <span class="o">&gt;</span> <span class="n">other</span><span class="p">)],</span>
            <span class="s">&#39;__ge__&#39;</span><span class="p">:</span> <span class="p">[(</span><span class="s">&#39;__le__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="p">(</span><span class="ow">not</span> <span class="bp">self</span> <span class="o">&gt;=</span> <span class="n">other</span><span class="p">)</span> <span class="ow">or</span> <span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">),</span>
                       <span class="p">(</span><span class="s">&#39;__gt__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="bp">self</span> <span class="o">&gt;=</span> <span class="n">other</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">),</span>
                       <span class="p">(</span><span class="s">&#39;__lt__&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="ow">not</span> <span class="bp">self</span> <span class="o">&gt;=</span> <span class="n">other</span><span class="p">)]</span>
        <span class="p">}</span>
        <span class="n">roots</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="nb">dir</span><span class="p">(</span><span class="n">cls</span><span class="p">))</span> <span class="o">&amp;</span> <span class="nb">set</span><span class="p">(</span><span class="n">convert</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">roots</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;must define at least one ordering operation: &lt; &gt; &lt;= &gt;=&#39;</span><span class="p">)</span>
        <span class="n">root</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">roots</span><span class="p">)</span>       <span class="c"># prefer __lt__ to __le__ to __gt__ to __ge__</span>
        <span class="k">for</span> <span class="n">opname</span><span class="p">,</span> <span class="n">opfunc</span> <span class="ow">in</span> <span class="n">convert</span><span class="p">[</span><span class="n">root</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">opname</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">roots</span><span class="p">:</span>
                <span class="n">opfunc</span><span class="o">.</span><span class="n">__name__</span> <span class="o">=</span> <span class="n">opname</span>
                <span class="n">opfunc</span><span class="o">.</span><span class="n">__doc__</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">opname</span><span class="p">)</span><span class="o">.</span><span class="n">__doc__</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">opname</span><span class="p">,</span> <span class="n">opfunc</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">cls</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.utils.functional</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>